package com.audaque.springboot.foshanupload.web.mvgenerator.service.impl;


import com.audaque.springboot.foshanupload.web.mvgenerator.co.EntityMbtplvVmConfigCo;
import com.audaque.springboot.foshanupload.web.mvgenerator.model.qo.GeneratorWhichVmQo;
import com.audaque.springboot.foshanupload.web.mvgenerator.properties.MvgeneratorDataSourceProperties;
import com.audaque.springboot.foshanupload.web.mvgenerator.service.MvGeneratorVmService;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zgb
 * @desc 这种写法最高使用3.4.1版本的生成器
 * @date 2021-06-11 11:11:50
 */
@Slf4j
@Service
public class MvGeneratorVmServiceImpl implements MvGeneratorVmService {
    //自定义
    @Autowired
    MvgeneratorDataSourceProperties mvgeneratorDataSourceProperties;


    //常改变量
    public static final boolean COVER_YN = true;
    public static final DbType DBTYPE = DbType.MYSQL;
    public static final String DBTYPENAME = DBTYPE.getDb();

    //dm
    //public static final String DBFIELD_FIX = "\"";
    //mysql
    public static final String DBFIELD_FIX = "`";


    //s:业务变量
    // 生成的文件存放的文件夹

    public static final String GENERATOR_OUTPUT = "generator-output";
    public static final String SERVICE_JAVA_PROJECT_PATH = "X:\\code\\java\\myDiy\\脚手架\\单体\\foshanupload\\mpdemo\\src\\main\\java";
    public static final String SERVICE_RESOURCES_PROJECT_PATH = "X:\\code\\java\\myDiy\\脚手架\\单体\\foshanupload\\mpdemo\\src\\main\\resources";
    public static final String API_JAVA_PROJECT_PATH = "X:\\code\\java\\myDiy\\脚手架\\单体\\foshanupload\\mpdemo\\src\\main\\java";

    public static final String PROJECT_NAME = "foshanupload";

    //e:业务变量


    // 自定义模板变量
    public static final String AUTHOR = "zhong.guobin";

    //父包
    public static final String PARENT_PACKAGE = "com.audaque.springboot.foshanupload.web";
    public static final String MODEL_NAME = "mpdemo";
    //端
    public static final String CLIENT = "Mpgt";
    public static final String CLIENT_URL = "mpgt";
    //表主键
    public static final String PRIMARY_KEY_FIELD = "user_id";
    public static final String PRIMARY_KEY_PROPERTY = "userId";
    public static final String PRIMARY_KEY_PROPERTY_UP = "UserId";



    private EntityMbtplvVmConfigCo getPojo() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        //固定
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/pojo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.pojo");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getQo() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/qo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.qo");
        return entityMbtplvVmConfigCo;
    }
    private EntityMbtplvVmConfigCo getReq() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/req.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.req");
        return entityMbtplvVmConfigCo;
    }

    private EntityMbtplvVmConfigCo getKbo() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/kbo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.kbo");
        return entityMbtplvVmConfigCo;
    }






    private EntityMbtplvVmConfigCo getApi() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/api.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".api");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getDao() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/idao.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".dao");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getDaoImpl() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/daoImpl.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".dao.impl");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getMapper() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);
        entityMbtplvVmConfigCo.setIsAll(false);
        //固定
        entityMbtplvVmConfigCo.setMapperName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperTemplate("vmtpl/mapper/mapper.java.vm");
        entityMbtplvVmConfigCo.setMapperPackage(CLIENT_URL + ".mapper");
        return entityMbtplvVmConfigCo;
    }

    private EntityMbtplvVmConfigCo getMapperXml() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setIsAll(false);

        entityMbtplvVmConfigCo.setMapperXmlProjectPath(SERVICE_RESOURCES_PROJECT_PATH);
        entityMbtplvVmConfigCo.setMapperXmlName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperXmlTemplate("vmtpl/mapper/mapper.xml.vm");
        entityMbtplvVmConfigCo.setMapperXmlPackage(CLIENT_URL + ".mapper");


        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/pojo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.pojo");

        //固定
        entityMbtplvVmConfigCo.setMapperName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperTemplate("vmtpl/mapper/mapper.java.vm");
        entityMbtplvVmConfigCo.setMapperPackage(CLIENT_URL + ".mapper");
        return entityMbtplvVmConfigCo;
    }




    private EntityMbtplvVmConfigCo getBaseSqlMapperXml() {

        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setIsAll(false);

        entityMbtplvVmConfigCo.setMapperXmlProjectPath(SERVICE_RESOURCES_PROJECT_PATH);
        entityMbtplvVmConfigCo.setMapperXmlName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperXmlTemplate("vmtpl/mapper/base-sql/mapper.xml.vm");
        entityMbtplvVmConfigCo.setMapperXmlPackage(CLIENT_URL + ".mapper.base-sql");


        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/pojo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.pojo");

        //固定
        entityMbtplvVmConfigCo.setMapperName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperTemplate("vmtpl/mapper/mapper.java.vm");
        entityMbtplvVmConfigCo.setMapperPackage(CLIENT_URL + ".mapper");
        return entityMbtplvVmConfigCo;
    }

    private EntityMbtplvVmConfigCo getKeyValueMapperXml() {

        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setIsAll(false);

        entityMbtplvVmConfigCo.setMapperXmlProjectPath(SERVICE_RESOURCES_PROJECT_PATH);
        entityMbtplvVmConfigCo.setMapperXmlName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperXmlTemplate("vmtpl/mapper/key-value/mapper.xml.vm");
        entityMbtplvVmConfigCo.setMapperXmlPackage(CLIENT_URL + ".mapper.key-value");


        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/pojo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.pojo");

        //固定
        entityMbtplvVmConfigCo.setMapperName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperTemplate("vmtpl/mapper/mapper.java.vm");
        entityMbtplvVmConfigCo.setMapperPackage(CLIENT_URL + ".mapper");
        return entityMbtplvVmConfigCo;
    }

    private EntityMbtplvVmConfigCo getQoMapperXml() {

        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setIsAll(false);

        entityMbtplvVmConfigCo.setMapperXmlProjectPath(SERVICE_RESOURCES_PROJECT_PATH);
        entityMbtplvVmConfigCo.setMapperXmlName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperXmlTemplate("vmtpl/mapper/qo/mapper.xml.vm");
        entityMbtplvVmConfigCo.setMapperXmlPackage(CLIENT_URL + ".mapper.qo");


        //固定
        entityMbtplvVmConfigCo.setEntityName("%s");
        entityMbtplvVmConfigCo.setEntityTemplate("vmtpl/pojo.java.vm");
        entityMbtplvVmConfigCo.setEntityPackage(CLIENT_URL + ".model.pojo");

        //固定
        entityMbtplvVmConfigCo.setMapperName("%s" + CLIENT + "Mapper");
        entityMbtplvVmConfigCo.setMapperTemplate("vmtpl/mapper/mapper.java.vm");
        entityMbtplvVmConfigCo.setMapperPackage(CLIENT_URL + ".mapper");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getService() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);


        //固定
        entityMbtplvVmConfigCo.setServiceName("%s" + CLIENT + "Service");
        entityMbtplvVmConfigCo.setServiceTemplate("vmtpl/iservice.java.vm");
        entityMbtplvVmConfigCo.setServicePackage(CLIENT_URL + ".service");
        return entityMbtplvVmConfigCo;
    }

    private EntityMbtplvVmConfigCo getServiceImpl() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);


        //固定
        entityMbtplvVmConfigCo.setServiceImplName("%s" + CLIENT + "ServiceImpl");
        entityMbtplvVmConfigCo.setServiceImplTemplate("vmtpl/serviceImpl.java.vm");
        entityMbtplvVmConfigCo.setServiceImplPackage(CLIENT_URL + ".service.impl");
        return entityMbtplvVmConfigCo;
    }


    private EntityMbtplvVmConfigCo getController() {
        EntityMbtplvVmConfigCo entityMbtplvVmConfigCo = new EntityMbtplvVmConfigCo();
        entityMbtplvVmConfigCo.setJavaProjectPath(SERVICE_JAVA_PROJECT_PATH);


        //固定
        entityMbtplvVmConfigCo.setControllerName("%s" + CLIENT + "Action");
        entityMbtplvVmConfigCo.setControllerTemplate("vmtpl/controller.java.vm");
        entityMbtplvVmConfigCo.setControllerPackage(CLIENT_URL + ".action");
        return entityMbtplvVmConfigCo;
    }

    private void executeEntity(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String javaProjectPath = entityMbtplvVmConfigCo.getJavaProjectPath();
        String projectPath = javaProjectPath;
        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }
        globalConfig.setEntityName(entityMbtplvVmConfigCo.getEntityName());
        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        packageInfo.setEntity(entityMbtplvVmConfigCo.getEntityPackage());
        TemplateConfig templateConfig = autoGenerator.getTemplate();
        templateConfig.setEntity(entityMbtplvVmConfigCo.getEntityTemplate());


    }


    private void executeMapperXml(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String mapperXmlProjectPath = entityMbtplvVmConfigCo.getMapperXmlProjectPath();
        String projectPath = mapperXmlProjectPath;

        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }
        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        TemplateConfig templateConfig = autoGenerator.getTemplate();

        globalConfig.setEntityName(entityMbtplvVmConfigCo.getEntityName());
        globalConfig.setXmlName(entityMbtplvVmConfigCo.getMapperXmlName());
        globalConfig.setMapperName(entityMbtplvVmConfigCo.getMapperName());

        packageInfo.setEntity(entityMbtplvVmConfigCo.getEntityPackage());
        packageInfo.setXml(entityMbtplvVmConfigCo.getMapperXmlPackage());
        packageInfo.setMapper(entityMbtplvVmConfigCo.getMapperPackage());

        templateConfig.setEntity(entityMbtplvVmConfigCo.getEntityTemplate());
        templateConfig.setXml(entityMbtplvVmConfigCo.getMapperXmlTemplate());
        templateConfig.setMapper(entityMbtplvVmConfigCo.getMapperTemplate());

    }


    private void executeMapper(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String javaProjectPath = entityMbtplvVmConfigCo.getJavaProjectPath();
        String projectPath = javaProjectPath;

        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }
        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        TemplateConfig templateConfig = autoGenerator.getTemplate();


        globalConfig.setMapperName(entityMbtplvVmConfigCo.getMapperName());


        packageInfo.setMapper(entityMbtplvVmConfigCo.getMapperPackage());


        templateConfig.setMapper(entityMbtplvVmConfigCo.getMapperTemplate());

    }


    private void executeService(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String javaProjectPath = entityMbtplvVmConfigCo.getJavaProjectPath();
        String projectPath = javaProjectPath;

        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }

        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        TemplateConfig templateConfig = autoGenerator.getTemplate();


        globalConfig.setServiceName(entityMbtplvVmConfigCo.getServiceName());
        packageInfo.setService(entityMbtplvVmConfigCo.getServicePackage());
        templateConfig.setService(entityMbtplvVmConfigCo.getServiceTemplate());
    }


    private void executeServiceImpl(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String javaProjectPath = entityMbtplvVmConfigCo.getJavaProjectPath();
        String projectPath = javaProjectPath;

        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }


        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        TemplateConfig templateConfig = autoGenerator.getTemplate();


        globalConfig.setServiceImplName(entityMbtplvVmConfigCo.getServiceImplName());

        packageInfo.setServiceImpl(entityMbtplvVmConfigCo.getServiceImplPackage());

        templateConfig.setServiceImpl(entityMbtplvVmConfigCo.getServiceImplTemplate());
    }


    private void executeController(EntityMbtplvVmConfigCo entityMbtplvVmConfigCo, AutoGenerator autoGenerator) {
        String javaProjectPath = entityMbtplvVmConfigCo.getJavaProjectPath();
        String projectPath = javaProjectPath;

        GlobalConfig globalConfig = autoGenerator.getGlobalConfig();
        if (COVER_YN) {
            globalConfig.setOutputDir(projectPath);
        } else {
            globalConfig.setOutputDir(projectPath + "/" + GENERATOR_OUTPUT);
        }


        PackageConfig packageInfo = autoGenerator.getPackageInfo();
        TemplateConfig templateConfig = autoGenerator.getTemplate();


        globalConfig.setControllerName(entityMbtplvVmConfigCo.getControllerName());

        packageInfo.setController(entityMbtplvVmConfigCo.getControllerPackage());

        templateConfig.setController(entityMbtplvVmConfigCo.getControllerTemplate());
    }


    public void entityRelationAllExecute(Boolean canAllTableYn, List<String> tableList) {
        AutoGenerator autoGenerator = null;
        //因为需要生成在java
        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getPojo(), autoGenerator);
        log.debug("finish:" + "pojo");
        autoGenerator.execute();


        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getQo(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "qoYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getReq(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "reqYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getKbo(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "kboYn");



        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getApi(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "apiYn");


        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getDao(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "daoYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getDaoImpl(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "daoImplYn");


        //因为需要生成在resources
        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "mapperXmlYn");




        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getBaseSqlMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "baseSqlMapperXmlYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getKeyValueMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "keyValueMapperXmlYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getQoMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "qoMapperXmlYn");

    }

    public void mscYnExecute(Boolean canAllTableYn, List<String> tableList) {
        //因为需要生成在java
        AutoGenerator autoGenerator = common(canAllTableYn, tableList);
        executeMapper(getMapper(), autoGenerator);
        log.debug("finish:" + "mapperYn");
        executeMapper(getMapperXml(), autoGenerator);
        log.debug("finish:" + "mapperXmlYn");
        executeService(getService(), autoGenerator);
        log.debug("finish:" + "serviceYn");
        executeServiceImpl(getServiceImpl(), autoGenerator);
        log.debug("finish:" + "serviceImplYn");
        executeController(getController(), autoGenerator);
        log.debug("finish:" + "controllerYn");
        autoGenerator.execute();
    }

    public void allExecute(Boolean canAllTableYn, List<String> tableList) {
        AutoGenerator autoGenerator = null;
        //因为需要生成在java
        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getQo(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "qoYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getReq(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "reqYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getKbo(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "kboYn");




        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getApi(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "apiYn");


        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getDao(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "daoYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getDaoImpl(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "daoImplYn");


        //因为需要生成在resources
        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "mapperXmlYn");



        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getBaseSqlMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "baseSqlMapperXmlYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getKeyValueMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "keyValueMapperXmlYn");

        autoGenerator = common(canAllTableYn, tableList);
        executeMapperXml(getQoMapperXml(), autoGenerator);
        autoGenerator.execute();
        log.debug("finish:" + "qoMapperXmlYn");

        //因为需要生成在java
        autoGenerator = common(canAllTableYn, tableList);
        executeEntity(getPojo(), autoGenerator);
        log.debug("finish:" + "pojo");
        executeMapper(getMapper(), autoGenerator);
        log.debug("finish:" + "mapperYn");
        executeService(getService(), autoGenerator);
        log.debug("finish:" + "serviceYn");
        executeServiceImpl(getServiceImpl(), autoGenerator);
        log.debug("finish:" + "serviceImplYn");
        executeController(getController(), autoGenerator);
        log.debug("finish:" + "controllerYn");
        autoGenerator.execute();

    }


    @Override
    public void doStart(GeneratorWhichVmQo generatorWhic) throws Exception {
        Boolean enabledYn = generatorWhic.getEnabledYn();
        if (!enabledYn) {
            return;
        }
        Boolean canAllTableYn = generatorWhic.getCanAllTableYn();
        List<String> tableList = generatorWhic.getTableList();
        Boolean allYn = generatorWhic.getAllYn();


        Boolean entityRelationAllYn = generatorWhic.getEntityRelationAllYn();
        Boolean mscYn = generatorWhic.getMscYn();
        Boolean pojoYn = generatorWhic.getPojoYn();
        Boolean qoYn = generatorWhic.getQoYn();
        Boolean reqYn = generatorWhic.getReqYn();
        Boolean kboYn = generatorWhic.getKboYn();

        Boolean apiYn = generatorWhic.getApiYn();
        Boolean daoYn = generatorWhic.getDaoYn();
        Boolean daoImplYn = generatorWhic.getDaoImplYn();
        Boolean mapperYn = generatorWhic.getMapperYn();
        Boolean mapperXmlYn = generatorWhic.getMapperXmlYn();

        Boolean serviceYn = generatorWhic.getServiceYn();
        Boolean serviceImplYn = generatorWhic.getServiceImplYn();
        Boolean controllerYn = generatorWhic.getControllerYn();


        List<EntityMbtplvVmConfigCo> entityMbtplvVmConfigCoList = new ArrayList<>();

        if (allYn) {
            allExecute(canAllTableYn, tableList);
        } else if (entityRelationAllYn) {
            entityRelationAllExecute(canAllTableYn, tableList);
        } else if (mscYn) {
            mscYnExecute(canAllTableYn, tableList);
        } else {

            if (pojoYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getPojo(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "pojo");
            }


            if (qoYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getQo(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "qoYn");
            }
            if (reqYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getReq(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "reqYn");
            }

            if (kboYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getKbo(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "kboYn");
            }




            if (apiYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getApi(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "apiYn");
            }


            if (daoYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getDao(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "daoYn");
            }
            if (daoImplYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeEntity(getDaoImpl(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "daoImplYn");
            }


            if (mapperXmlYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeMapperXml(getMapperXml(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "mapperXmlYn");



                autoGenerator = common(canAllTableYn, tableList);
                executeMapperXml(getBaseSqlMapperXml(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "baseSqlMapperXmlYn");

                autoGenerator = common(canAllTableYn, tableList);
                executeMapperXml(getKeyValueMapperXml(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "keyValueMapperXmlYn");

                autoGenerator = common(canAllTableYn, tableList);
                executeMapperXml(getQoMapperXml(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "qoMapperXmlYn");


            }


            if (mapperYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeMapper(getMapper(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "mapperYn");
            }


            if (serviceYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeService(getService(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "serviceYn");
            }

            if (serviceImplYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeServiceImpl(getServiceImpl(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "serviceImplYn");
            }


            if (controllerYn) {
                AutoGenerator autoGenerator = common(canAllTableYn, tableList);
                executeController(getController(), autoGenerator);
                autoGenerator.execute();
                log.debug("finish:" + "controllerYn");

            }


        }


    }


    private AutoGenerator common(Boolean canAllTableYn, List<String> tableList) {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.setAuthor(AUTHOR);
        globalConfig.setOpen(false); // 生成后是否打开资源管理器
        globalConfig.setFileOverride(true); // 重新生成时文件是否覆盖

        // 自定义文件命名，注意 %s 会自动填充表实体属性！


        globalConfig.setIdType(IdType.INPUT); // 主键策略
        globalConfig.setDateType(DateType.ONLY_DATE); // 定义生成的实体类中日期类型
        globalConfig.setSwagger2(true); // 开启Swagger2模式
        mpg.setGlobalConfig(globalConfig);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(mvgeneratorDataSourceProperties.getUrl());
        dsc.setDriverName(mvgeneratorDataSourceProperties.getDriverClassName());
        dsc.setUsername(mvgeneratorDataSourceProperties.getUsername());
        dsc.setPassword(mvgeneratorDataSourceProperties.getPassword());
        dsc.setDbType(DBTYPE);
        // 类型转换
/*    dsc.setTypeConvert(
            new ITypeConvert() {
              @Override
              public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                String t = fieldType.toLowerCase();
                if (t.contains("tinyint(1)")) {
                  return DbColumnType.INTEGER;
                }
                // 其它字段采用默认转换（非mysql数据库可以使用其它默认的数据库转换器）
                return new MySqlTypeConvert().processTypeConvert(globalConfig, fieldType);
              }
            });*/
        mpg.setDataSource(dsc);


        // 包配置
        PackageConfig packageInfo = new PackageConfig();
        packageInfo.setModuleName(MODEL_NAME); // 模块名
        packageInfo.setParent(PARENT_PACKAGE);

        mpg.setPackageInfo(packageInfo);


        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表映射到实体的命名策略
        //strategy.setTablePrefix(ZgbActiviti7webCodeGenerator.MODEL_NAME + "_"); // 设置表前缀不生成
        //
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        //strategy.setLogicDeleteFieldName("deleted_flag"); // 逻辑删除字段名
        //strategy.setEntityBooleanColumnRemoveIsPrefix(true); // 去掉布尔值的is_前缀

        // 自动填充
  /*      TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);*/

        strategy.setRestControllerStyle(true); // restful api风格控制器
        strategy.setControllerMappingHyphenStyle(false); // url中驼峰转连字符


        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        tableList = ListUtils.emptyIfNull(tableList);
        if (!canAllTableYn) {
            if (!tableList.isEmpty()) {
                String[] tableArr = tableList.toArray(new String[tableList.size()]);
                strategy.setInclude(tableArr); // 需要生成的表,数组，可一次生成多个表
            } else {
                strategy.setInclude("");
            }

        }


        // e:改表++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // s:mybatisplus重构++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // 自定义实体父类
        //strategy.setSuperEntityClass(ZgbActiviti7webCodeGenerator.SUPER_ENTITY_CLASS);
        // 自定义实体，公共字段
        //strategy.setSuperEntityColumns(ZgbActiviti7webCodeGenerator.SUPER_ENTITY_COLUMN_ARR);

        // 自定义 mapper 父类
        //strategy.setSuperMapperClass(ZgbActiviti7webCodeGenerator.SUPER_MAPPER_CLASS);
        // 自定义 service 父类
        //strategy.setSuperServiceClass(ZgbActiviti7webCodeGenerator.SUPER_SERVICE_INTERF_CLASS);
        // 自定义 service 实现类父类
        //strategy.setSuperServiceImplClass(ZgbActiviti7webCodeGenerator.SUPER_SERVICE_IMPL_CLASS);

        mpg.setStrategy(strategy);

        // 6.注入自定义配置，可以在 VM 中使用 cfg.abc 设置的值
        InjectionConfig cfg =
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("AUTHOR", AUTHOR);
                        map.put("LOCALDATETIMENOW", LocalDateTime.now().toString());
                        map.put("PROJECT_NAME", PROJECT_NAME);
                        map.put("MODEL_NAME", MODEL_NAME);
                        map.put("CLIENT", CLIENT);
                        map.put("CLIENT_URL", CLIENT_URL);
                        map.put("PARENT_PACKAGE", PARENT_PACKAGE);
                        map.put("DBTYPENAME", DBTYPENAME);
                        map.put("DBFIELD_FIX", DBFIELD_FIX);
                        map.put("PRIMARY_KEY_FIELD", PRIMARY_KEY_FIELD);
                        map.put("PRIMARY_KEY_PROPERTY", PRIMARY_KEY_PROPERTY);
                        map.put("PRIMARY_KEY_PROPERTY_UP", PRIMARY_KEY_PROPERTY_UP);

                        this.setMap(map);
                    }
                };
        mpg.setCfg(cfg);

        // 7.自定义模板配置，可以 copy 源码 mybatis-plus/src/main/resources/template 下面内容修改，
        // 放置自己项目的 src/main/resources/template 目录下, 默认名称一下可以不配置，也可以自定义模板名称
        TemplateConfig templateConfig = new TemplateConfig();
        //不使用框架自带的模板
        templateConfig.setEntity("");
        templateConfig.setXml("");
        templateConfig.setMapper("");
        templateConfig.setService("");
        templateConfig.setServiceImpl("");
        templateConfig.setController("");

        mpg.setTemplate(templateConfig);
        return mpg;
    }

}
